<?php
namespace Application\Api\Controller;

class File extends Base
{

    public function callbackAction()
    {
        $post = $this->getPostData();
        $header = $this->getHeaderData();
        $server = $this->getServerData();
        $serviceOss = $this->get(SERVICE_OSS);
        $body = http_build_query($post);
        $result = $serviceOss->checkCallback($header['authorization'], $header['x-oss-pub-key-url'], $server['request_uri'], $body);
        if(!$result){
            $this->sendFail('FAIL', 'oss回调验证失败');
        }
        /*入库 start*/
        $serviceOssFile = $this->get(SERVICE_OSSFILE);
        $insert = [
            'dry_prefix' => $post['prefix'],
            'dry_name' => $post['name'],
            'dry_object' => $post['object'],
            'dry_mime_type' => $post['mimeType'],
            'dry_format' => $post['imageInfoFormat'],
            'dry_width' => $post['imageInfoWidth'],
            'dry_height' => $post['imageInfoHeight'],
            'dry_size' => $post['size']
        ];
        if($insert['dry_width'] == 0){
            $insert['dry_width'] = $post['vw'];
            $insert['dry_height'] = $post['vh'];
        }
        $insert = addDateTime($insert, THE_DATE_TIME);
        $id = $serviceOssFile->add($insert);
        $post['id'] = $id;
        /*入库 end*/
        $this->sendSuccess($post);
    }

    public function getListByCategoryAction()
    {
        $data = $this->getRequestData();
        $serviceFile = $this->get(SERVICE_FILE);
        $extension = [
            'categoryId' => $data['categoryId']
        ];
        $list = $serviceFile->getListByCategory($data['page'], $data['limit'], $extension);
        $this->sendSuccess($list);
    }

    public function moveAction()
    {
        $data = $this->getRequestData();
        $serviceFile = $this->get(SERVICE_FILE);
        $serviceFile->move($data['idIn'], $data['categoryId']);
        $this->sendSuccess();
    }

    public function deleteAction()
    {
        $data = $this->getRequestData();
        $serviceFile = $this->get(SERVICE_FILE);
        $serviceFile->deletePlus($data['idIn']);
        $this->sendSuccess();
    }

    public function renameAction()
    {
        $data = $this->getRequestData();
        $serviceFile = $this->get(SERVICE_FILE);
        $serviceFile->rename($data['name'], $data['id']);
        $this->sendSuccess();
    }

    public function getListByIdsAction()
    {
        $data = $this->getRequestData();
        $serviceFile = $this->get(SERVICE_FILE);
        $list = $serviceFile->getListByIds($data['dataIn']);
        $data = [
            'domain' => getOssDomain(),
            'list' => $list
        ];
        $this->sendSuccess($data);
    }

}